<?php
namespace User\Controller;

use Think\Controller;
/***
 * Class SubordinateController
 * @package User\Controller
 *
 *      下级管理
 *  ->下级mt4账号列表
 *  ->下级mt4交易历史
 *  ->下级mt4在途订单
 *  ->下级mt4限价订单
 *  ->下级mt4出金操作
 */


class SubordinateController extends  Controller{
    //获所有下级，只有账号
    public function getChilds($data,$uid,$arr){
        foreach ($data as $k => $v){
            if($uid==$v['user_recommend']){
                unset($data[$k]);
                $arr[]=$v['user_id'];
                $arr=$this->getChilds($data,$v['user_id'],$arr);
            }
        }
        return $arr;
    }
    //获取下线层级
    public function getChildsClass($data,$uid,$arr,$n){
        $n++;
        foreach ($data as $k => $v){
            if($uid==$v['user_recommend']){
                $v['class']=$n;
                $arr[]=$v;
                unset($data[$k]);
                $arr=$this->getChildsClass($data,$v['user_id'],$arr,$n);
            }
        }
        return $arr;
    }
    //我的结构图
    public function My_Mt4List(){
        $user = session('user');
        if(empty($user)){echo "身份信息过期,请重新登录";exit;}
        $position = M('position')->where(array('id'=>$user['position']))->field('id,user_position')->find();//查询职位
        $this->assign('position',$position);
        $this->assign('msg',$user);
        $this->display();
    }
    //下载结构图
    public function downloadTree(){
        $id=I('id') ? I('id') : die('{\'status\':0,\'msg\':\'非法操作\'}');
        $model=M('user');
        $users=$model->field('user.user_id,user.user_name,user.user_recommend,user.user_email,position.user_position,user.level')
            ->join('position ON user.position = position.id','left')->where('user.user_recommend >0')->select();
        $self=$model->field('user.user_id,user.user_name,user.user_recommend,user.user_email,position.user_position,user.level')
            ->join('position ON user.position = position.id','left')->where(['user.user_id'=>$id])->find();
        $arr[]=$self;
        $list=$this->getChildsClass($users,$id,$arr,0);
        $csv=new \Think\Csv();
        $csv_title=array('ID','Name','Agent','Email','	Position','Leverage','Class');
        $csv->put_csv($list,$csv_title);
    }
    //获取用户结构图-分批获取，用户量大的时候使用
    public function getTree2(){
        $id=I('post.id') ? I('post.id') : die(json_encode(array('status'=>0,'msg'=>'非法操作')));
        $model=M('user');
        $users=$model->field('user.user_id,user.user_recommend,user.user_name,position.user_position')
            ->join('position ON user.position = position.id','left')->where('user.user_recommend = '.$id)->select();
        if(empty($users)){
            $this->ajaxReturn(array('status'=>0,'msg'=>'到底了！！！'));
        }else{
            $logins=[];
            foreach($users as $k => $v){
                $logins[]=$v['user_id'];
            }
            $agents=$model->field('distinct user_recommend')->where('user_recommend in ('.implode(',',$logins).')')->select();
            foreach($users as $k => $v){
                $users[$k]['hasChild']=0;
                foreach ($agents as $ke => $va){
                    if($v['user_id']==$va['user_recommend']){
                        $users[$k]['hasChild']=1;break;
                    }
                }
            }
            $this->ajaxReturn(array('status'=>1,'msg'=>$users));
        }
    }
    //下级MT4列表
    public function Mt4List(){
        $user = session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}    //session为空跳转到登陆页
        $list = M('user')->field('user_id,name_zh_cn,user_email,position,level,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile='Uploads/getChildsClass/'.date('Ymd').'-'.$user['user_id'].'.txt';

        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChildsClass($list,$user['user_id'],[],0);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
            //$this->ajaxReturn(array('status'=>0,'msg'=>'缓存信息已更新，请重新载入'));
        }
        $position = M('position')->select();
        $this->assign('position',$position);
        $this->assign('user',$user);
        $this->assign('mt4_list',$mt4_list);
        $this->display();
    }
    //下载客户数据
    public function UserList_download(){
        $user = session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}    //session为空跳转到登陆页
        $list = M('user')->cache(true)->field('user_id,name_zh_cn,user_email,user_cards,user_createtime,position,level,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $position = M('position')->select();//职位信息
        $childrenFile='Uploads/getChildsClass/'.date('Ymd').'-'.$user['user_id'].'.txt';

        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChildsClass($list,$user['user_id'],[],0);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        $data_csv=[];
        foreach($mt4_list as $k=>$v){
            $data_csv[$k]['user_id'] = $v['user_id'];//账号
            $data_csv[$k]['name_zh_cn'] = $v['name_zh_cn'];//中文姓名
            $data_csv[$k]['user_cards'] = $v['user_cards'];//身份证号
            $data_csv[$k]['user_email'] = $v['user_email'];//邮箱
            $data_csv[$k]['user_createtime'] = $v['user_createtime'];//注册时间
            foreach($position as $key => $value){
                if($value['id']==$v['position']){
                    $data_csv[$k]['position'] = $value['user_position'];//职位
                }
            }
            $data_csv[$k]['class'] = $v['class'];//层级
            $data_csv[$k]['level'] = $v['level'];//杠杆
            $data_csv[$k]['user_recommend'] = $v['user_recommend'];//推荐人
        }
        $csv=new \Think\Csv();
        $csv_title=array('账号','中文姓名','身份证号','邮箱','注册时间','职位','层级','杠杆','推荐人');
        $csv->put_csv($data_csv,$csv_title,$user['user_id']."-客户数据");
    }
    //查询用户详细
    public function Mt4_detailed(){
        $user=session('user');
        if(empty($user)){
            die( "信息过期");
        }
        $where['LOGIN'] = I('LOGIN');
        if(empty($where['LOGIN'])){echo "信息获取失败";exit;}
        $year=date("Y",time());
        $fo=$year."-01-01 00:00:00";
        $to=$year."-12-31 23:59:59";
        $where['CLOSE_TIME'] = array('between',array($fo,$to));
        $where1['CMD'] = 6;                              $where2['CMD'] = 6;
        $where1['COMMENT'] = array('like','deposit%');  $where2['COMMENT'] = array('like','withdrawal%');
        $mt4_trades = M(' mt4_trades', '', 'DB_CONFIG2');//数据库
//        if($user['type']==1){       //1-数字 2-外汇
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//        }else{
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//        }

        $deposit = $mt4_trades->cache(true)->where($where)->where($where1)->sum('PROFIT');   //一年总入金
        $withdrawal = $mt4_trades->cache(true)->where($where)->where($where2)->sum('PROFIT');//一年总出金
        
        $where['CMD'] = array('between',array(0,1));//买涨买跌
		$year_profit     = $mt4_trades->where($where)->sum('PROFIT');//一年总获利
        //查询每个交易品种交易量
        $each = $mt4_trades->cache(true)->where($where)->group('SYMBOL')->field('LOGIN,sum(VOLUME) as volume,sum(PROFIT) as profit,SYMBOL,count(SYMBOL) as count')->select();
        $list = $mt4_trades->where($where)->field('SUM(`VOLUME`) as volume,DATE_FORMAT(CLOSE_TIME,"%Y-%m") as time')->group('time')->select(); //每月交易量 .将时间格式化成月，然后按月group  //DATE_FORMAT(CLOSE_TIME,"%Y-%m") as months,sum(VOLUME)\
        
		$data=[];
        for($i=1;$i<=12;$i++){
            $month=$i>10 ? $i : '0'.$i;
            $data[]=array('month'=>$year.'-'.$month,'volume'=>0,'class'=>$i-1);
        }
        foreach ($data as $k => $v){
            foreach ($list as$ke => $va){
                if($v['month']==$va['time']){
                    $data[$k]['volume']=round($va['volume']/100,2);
                }
            }
        }
        //交易历史
        $count = $mt4_trades->where($where)->count();
        $Page = new \Think\Page($count, 10);
        $history = $mt4_trades->where($where)->order('TICKET desc')
            ->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $show = $Page->show();// 分页显示输出
       $this->assign('history',$history);
       $this->assign('data',json_encode($data));
       $this->assign('deposit',$deposit);
       $this->assign('withdrawal',$withdrawal);
       $this->assign('profit',$year_profit);
       $this->assign('each',$each);
       $this->assign('page',$show);
       $this->display();
    }
    //客户交易历史
    public function UserTrade_history(){
        $user =session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}    //session为空跳转到登陆页
        $fo= I('fo')?I('fo'):'';
        $to= I('to')?I('to'):'';
        $type= I('type')?I('type'):'';
        $login = I('login')?I('login'):'';
        $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//        if($user['type']==1){       //1-数字 2-外汇
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//        }else{
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//        }
        $where = '';
        $where['CMD'] = array('between',array(0,1));      //买涨买跌
		
        if(!empty($fo) && !empty($to)){
            $where['CLOSE_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }else{
			$where['CLOSE_TIME'] = array('NEQ','1970-01-01 00:00:00');
		}
        if($type==1){
            $where['CMD'] = 0;
        }elseif($type==2){
            $where['CMD'] = 1;
        }else{
            $where['CMD'] = array('lt',2);
        }
        //获取下线
        $list = M('user')->field('user_id,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile='Uploads/getChilds/'.date('Ymd').'-'.$user['user_id'].'.txt';
        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChilds($list,$user['user_id'],[]);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        if(!empty($login)){
            if (in_array($login, $mt4_list)){
                $where['LOGIN'] = $login;
            }else{
                echo "您查询的MT4账号不是您的客户";exit;
            }
        }else{
            $section=C('section');//获取号段
            $where['LOGIN']=array(array('in',$mt4_list ,array('between',array($section['min'],$section['max']))));
        }
        if(!empty($where)){// && ($user['position']==10)
            //统计交易量
            $sum_num = $mt4_trades->field('sum(VOLUME) as volume,sum(COMMISSION) as commission,sum(SWAPS) as swaps,sum(PROFIT) as profit')->where($where)->select();
            //交易历史
            $count = $mt4_trades->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['fo']    = $fo;
            $Page->parameter['to']    = $to;
            $Page->parameter['login'] = $login;
            $Page->parameter['type'] = $type;
            $history = $mt4_trades->where($where)->order('TICKET desc')
                ->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,`COMMISSION`,SWAPS,PROFIT,`COMMENT`')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
            $show = $Page->show();// 分页显示输出
            $this->assign('fo',$fo);
            $this->assign('to',$to);
            $this->assign('login',$login);
            $this->assign('type',$type);
            $this->assign('sum_num',$sum_num);
            $this->assign('history',$history);
            $this->assign('page',$show);
            $this->display();
        }else{
            $this->display();
        }
    }
    //下载客户交易历史
    public function UserTrade_history_download(){
        $user = session('user');//if($user['position']!=10){$this->error('您不是代理商，没有权限');}
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        //获取下线
        $list = M('user')->field('user_id,name_zh_cn,user_email,position,level,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile='Uploads/getChilds/'.date('Ymd').'-'.$user['user_id'].'.txt';
        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChilds($list,$user['user_id'],[]);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to').' 23:59:59':'';
        $type = I('type')?I('type'):'';
        $login = I('login')?I('login'):'';
        $where = '';
        if(empty($fo) || empty($to)){
            echo "请查询数据后下载";exit;
			//
        }else{
			//$where['CLOSE_TIME'] = array('NEQ','1970-01-01 00:00:00');
			$where['CLOSE_TIME'] = array('between',array($fo,$to));
		}
        if(empty($type)){
            $where['CMD'] = array('lt',2);
        }elseif($type==1){
            $where['CMD'] = 0;
        }else{
            $where['CMD'] = 1;
        }
        if(empty($login)){
            $section=C('section');//获取号段
            $where['LOGIN']=array(array('in',$mt4_list ),array('between',array($section['min'],$section['max'])));
        }else{
            if (in_array($login, $mt4_list)){
                $where['LOGIN'] = $login;
            }else{
                echo "您查询的MT4账号不是您的客户";exit;
            }
        }
        //$where['SYMBOL']  = array('like','%bo%');
        $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//        if($user['type']==1){       //1-数字 2-外汇
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//        }else{
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//        }

        $data = $mt4_trades->where($where)->order('TICKET desc')->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')->select();
        $data_csv= [];
        foreach ($data as $k=>$v){
            $data_csv[$k]['ticket']     = $v['ticket'];                  //订单号
            $data_csv[$k]['login']      = $v['login'];                   //mt4账号
            $data_csv[$k]['symbol']     = $v['symbol'];                  //交易品种
            if($v['cmd']==0){                                            //交易类型
                $data_csv[$k]['cmd'] = 'BUY';
            }else if($v['cmd']==1){
                $data_csv[$k]['cmd'] = 'SELL';
            }
            $data_csv[$k]['volume']      =  round($v['volume']/100,2);          //交易量
            $data_csv[$k]['open_time']   = $v['open_time'];           //开仓时间
            $data_csv[$k]['open_price']   = $v['open_price'];         //开仓价格
            $data_csv[$k]['sl'] = $v['sl'];
            $data_csv[$k]['tp'] = $v['tp'];
            $data_csv[$k]['close_time']  = $v['close_time'];            //关仓时间
            $data_csv[$k]['close_price']  = $v['close_price'];         //关仓价格
            $data_csv[$k]['commission'] = $v['commission'];            //手续费
            $data_csv[$k]['swaps'] =  $v['swaps'];                     //库存费
            $data_csv[$k]['profit'] = round($v['profit'],2); //获利
            $data_csv[$k]['comment'] = $v['comment'];                  //注释
        }
        $csv=new \Think\Csv();
        $csv_title=array('订单号','MT4','交易品种','交易类型','交易量','开仓时间','开仓价格','SL','TP','关仓时间','关仓价格','手续费','库存费','收益','注释');
        $csv->put_csv($data_csv,$csv_title,"客户交易历史");
    }
    //客户在途订单
    public function User_Ontheway_order(){
        $user = session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}    //session为空跳转到登陆页
        $fo= I('fo')?I('fo'):'';
        $to= I('to')?I('to'):'';
        $type= I('type')?I('type'):'';
        $login = I('login')?I('login'):'';//53381//53223
        $where = [];
		$where['CLOSE_TIME'] = '1970-01-01 00:00:00';
        if(!empty($fo) && !empty($to)){
            $where['OPEN_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }
        //获取下线
        $list = M('user')->field('user_id,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile ='Uploads/getChilds/'.date('Ymd').'-'.$user['user_id'].'.txt';
        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChilds($list,$user['user_id'],[]);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        if(!empty($login)){
            if (in_array($login, $mt4_list)){
                $where['LOGIN'] = $login;
            }else{
                echo "您查询的MT4账号不是您的客户";exit;
            }
        }else{
            $section=C('section');//获取号段
            $where['LOGIN']=array(array('in',$mt4_list ),array('between',array($section['min'],$section['max'])));
        }
        if($type==1){
            $where['CMD'] = 0;
        }elseif($type==2){
            $where['CMD'] = 1;
        }else{
            $where['CMD'] = array('lt',2);
        }

        /*if(!empty($login)){// && ($user['position']==10)
            $mt4_trades = M(' mt4_trades', '', 'DB_CONFIG2');   //数据库
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
            $count = $mt4_trades->where($where)->count();
            $Page = new \Think\Page($count, 15);
            $Page->parameter['fo']    = $fo;
            $Page->parameter['to']    = $to;
            $Page->parameter['login'] = $login;
            $Page->parameter['type'] = $type;
            $history = $mt4_trades->where($where)->order('TICKET desc')
                ->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,`COMMISSION`,SWAPS,PROFIT,`COMMENT`')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
            $show = $Page->show();  // 分页显示输出

            $this->assign('fo',$fo);
            $this->assign('to',$to);
            $this->assign('login',$login);
            $this->assign('type',$type);
            $this->assign('history',$history);
            $this->assign('page',$show);
            $this->display();
        }else{
            $this->display();
        }*/
		$mt4_trades = M(' mt4_trades', '', 'DB_CONFIG2');   //数据库
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
		$count = $mt4_trades->where($where)->count();
		$Page = new \Think\Page($count, 15);
		$Page->parameter['fo']    = $fo;
		$Page->parameter['to']    = $to;
		$Page->parameter['login'] = $login;
		$Page->parameter['type'] = $type;
		$history = $mt4_trades->where($where)->order('TICKET desc')
			->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,`COMMISSION`,SWAPS,PROFIT,`COMMENT`')
			->limit($Page->firstRow . ',' . $Page->listRows)
			->select();
		$show = $Page->show();  // 分页显示输出

		$this->assign('fo',$fo);
		$this->assign('to',$to);
		$this->assign('login',$login);
		$this->assign('type',$type);
		$this->assign('history',$history);
		$this->assign('page',$show);
		$this->display();
    }
    //下载在途订单
    public function User_Ontheway_order_download(){
        $user =session('user');//if($user['position']!=10){$this->error('您不是代理商，没有权限');}
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to'):'';
        $type = I('type')? I('type'):'';
        $login = I('login')?I('login'):'';
        $where = [];
		$where['CLOSE_TIME'] = '1970-01-01 00:00:00';
        if(!empty($fo) && !empty($to)){
            $where['OPEN_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(empty($type)){
            $where['CMD'] = array('lt',2);
        } elseif ($type==1) {
            $where['CMD'] = 0;
        } else {
            $where['CMD'] = 1;
        }
        //获取下线
        $list = M('user')->field('user_id,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile ='Uploads/getChilds/'.date('Ymd').'-'.$user['user_id'].'.txt';
        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChilds($list,$user['user_id'],[]);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        if(!empty($login)){
            if (in_array($login, $mt4_list)){
                $where['LOGIN'] = $login;
            }else{
                echo "您查询的MT4账号不是您的客户";exit;
            }
        }else{
            $section=C('section');//获取号段
            $where['LOGIN']=array(array('in',$mt4_list ),array('between',array($section['min'],$section['max'])));
        }

        $where['CLOSE_TIME'] = '1970-01-01 00:00:00';
        //$where['SYMBOL']     = array('like','%bo%');
        $mt4_trades = M(' mt4_trades','','DB_CONFIG2');
//        if($user['type']==1){       //1-数字 2-外汇
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//        }else{
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//        }
        $data = $mt4_trades->where($where)->order('TICKET desc')->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')->select();
        foreach ($data as $k=>$v){
            $data_csv[$k]['ticket']     = $v['ticket'];                 //订单号
            $data_csv[$k]['login']      = $v['login'];                  //mt4账号
            $data_csv[$k]['symbol']     = $v['symbol'];                 //交易品种
            if($v['cmd']==0){                                           //交易类型
                $data_csv[$k]['cmd'] = 'BUY';
            }else if($v['cmd']==1){
                $data_csv[$k]['cmd'] = 'SELL';
            }
            $data_csv[$k]['volume']      =  round($v['volume']/100,2);//交易量
            $data_csv[$k]['open_time']   = $v['open_time'];           //开仓时间
            $data_csv[$k]['open_price']   = $v['open_price'];         //开仓价格
            $data_csv[$k]['sl'] = $v['sl'];
            $data_csv[$k]['tp'] = $v['tp'];
            if(strtotime($v['close_time'])==-10800){                   //关仓时间
                $data_csv[$k]['close_time']  = '--  -- --';
            }
            $data_csv[$k]['close_price']  = $v['close_price'];          //关仓价格
            $data_csv[$k]['commission'] = $v['commission'];             //手续费
            $data_csv[$k]['swaps'] = $v['swaps'];                       //库存费
            $data_csv[$k]['profit'] =round($v['profit'],2);   //获利
            $data_csv[$k]['comment'] = $v['comment'];                    //注释
        }
        $csv=new \Think\Csv();
        $csv_title=array('订单号','MT4','交易品种','交易类型','交易量','开仓时间','开仓价格','SL','TP','关仓时间','关仓价格','手续费','库存费','收益','注释');
        $csv->put_csv($data_csv,$csv_title,"客户在途订单");
    }
    //客户限价订单
    public function User_Fixed_order(){
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to'):'';
        $type = I('type')?I('type'):'';
        $login = I('login')?I('login'):'';
        $where = '';
        if(!empty($fo) && !empty($to)){
            $where['OPEN_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(empty($type)){
            $where['CMD'] = array('between', array(2, 3));;
        }else{
            $where['CMD'] = $type;
        }
        //获取下线
        $list = M('user')->cache(true)->field('user_id,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile ='Uploads/getChilds/'.date('Ymd').'-'.$user['user_id'].'.txt';
        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChilds($list,$user['user_id'],[]);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        if(!empty($login)){
            if (in_array($login, $mt4_list)){
                $where['LOGIN'] = $login;
            }else{
                echo "您查询的MT4账号不是您的客户";exit;
            }
        }else{
            $section=C('section');//获取号段
            $where['LOGIN']=array(array('in',$mt4_list ),array('between',array($section['min'],$section['max'])));
        }

        if(empty($fo) && empty($to) && empty($type) && empty($login) && ($user['position']!=10)){
            $this->assign('user',$user);
            $this->display();
        }else{
            $mt4_trades = M(' mt4_trades','','DB_CONFIG2');
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
            $count =  $mt4_trades->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['fo']= $fo;
            $Page->parameter['to']= $to;
            $Page->parameter['type']= $type;
            $Page->parameter['login']= $login;
            $show = $Page->show();// 分页显示输出
            $data = $mt4_trades->where($where)->order('TICKET desc')
                ->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')
                ->limit($Page->firstRow . ',' . $Page->listRows)->select();
            $this->assign('type',$type);
            $this->assign('data',$data);
            $this->assign('to',$to);
            $this->assign('fo',$fo);
            $this->assign('user',$user);
            $this->assign('page',$show);
            $this->display();
        }
    }
    //下载客户限价订单
    public function User_Fixed_order_download(){
        $user =session('user');if($user['position']!=10){$this->error('您不是代理商，没有权限');}
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo')? I('fo'):'';
        $to = I('to')? I('to'):'';
        $type = I('type')?I('type'):'';
        $login = I('login')?I('login'):'';
        $where = '';

        //获取下线
        $list = M('user')->cache(true)->field('user_id,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile ='Uploads/getChilds/'.date('Ymd').'-'.$user['user_id'].'.txt';
        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChilds($list,$user['user_id'],[]);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        if(!empty($login)){
            if (in_array($login, $mt4_list)){
                $where['LOGIN'] = $login;
            }else{
                echo "您查询的MT4账号不是您的客户";exit;
            }
        }else{
            $section=C('section');//获取号段
            $where['LOGIN']=array(array('in',$mt4_list ),array('between',array($section['min'],$section['max'])));
        }

        if(empty($fo) || empty($to)){
            echo "请查询后下载";exit;
        }else{
            $where['OPEN_TIME'] = array('between',array($fo,$to.' 23:59:59'));
        }

        if(empty($type)){
            $where['CMD'] = array('between', array(2, 3));;
        }else{
            $where['CMD'] = $type;
        }
        $mt4_trades = M(' mt4_trades','','DB_CONFIG2');
//        if($user['type']==1){       //1-数字 2-外汇
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//        }else{
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//        }
        $data = $mt4_trades->where($where)->order('TICKET desc')->field('LOGIN,TICKET,SYMBOL,CMD,VOLUME,OPEN_TIME,OPEN_PRICE,CLOSE_TIME,CLOSE_PRICE,SL,TP,COMMISSION,SWAPS,PROFIT,COMMENT')->select();
        $data_csv= [];
        foreach ($data as $k=>$v){
            $data_csv[$k]['ticket']     = $v['ticket'];                  //订单号
            $data_csv[$k]['login']      = $v['login'];                   //mt4账号
            $data_csv[$k]['symbol']     = $v['symbol'];                  //交易品种
            if($v['cmd']==0){                                            //交易类型
                $data_csv[$k]['cmd'] = 'BUY';
            }else if($v['cmd']==1){
                $data_csv[$k]['cmd'] = 'SELL';
            }
            $data_csv[$k]['volume']      =  round($v['volume']/100,2);          //交易量
            $data_csv[$k]['open_time']   = $v['open_time'];           //开仓时间
            $data_csv[$k]['open_price']   = $v['open_price'];         //开仓价格
            $data_csv[$k]['sl'] = $v['sl'];
            $data_csv[$k]['tp'] = $v['tp'];
            $data_csv[$k]['close_time']  = $v['close_time'];            //关仓时间
            $data_csv[$k]['close_price']  = $v['close_price'];         //关仓价格
            $data_csv[$k]['commission'] = $v['commission'];            //手续费
            $data_csv[$k]['swaps'] =  $v['swaps'];                     //库存费
            $data_csv[$k]['profit'] = round($v['profit'],2); //获利
            $data_csv[$k]['comment'] = $v['comment'];                  //注释
        }
        $csv=new \Think\Csv();
        $csv_title=array('订单号','MT4','交易品种','交易类型','交易量','开仓时间','开仓价格','SL','TP','关仓时间','关仓价格','手续费','库存费','收益','注释');
        $csv->put_csv($data_csv,$csv_title,"客户限价订单");
    }
	
	//来往资金
    public function fund(){
        $user = session('user');
        if(empty($user)){echo "账号信息过期,请重新登录";exit;}
        $fo = I('fo') ? I('fo'):'';
        $to = I('to') ? I('to'): '';
		$login = I('login') ? I('login'): '';
        $where = '';
        if (!empty($fo) && !empty($to)) {
            $where['OPEN_TIME'] = array('between', array($fo, $to.' 23:59:59'));
			$this->assign('to', $to);
            $this->assign('fo', $fo);
        }
		
		//获取下线
        $list = M('user')->field('user_id,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile ='Uploads/getChilds/'.date('Ymd').'-'.$user['user_id'].'.txt';
        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChilds($list,$user['user_id'],[]);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        if(!empty($login)){
            if (in_array($login, $mt4_list)){
                $where['LOGIN'] = $login;
            }else{
                echo "您查询的MT4账号不是您的客户";exit;
            }
			$this->assign('login', $login);
        }else{
            $section=C('section');//获取号段
            $where['LOGIN']=array(array('in',$mt4_list ),array('between',array($section['min'],$section['max'])));
        }
		
		$where['CMD'] = 6;
		$mt4_trades = M(' mt4_trades', '', 'DB_CONFIG2');
//            if($user['type']==1){       //1-数字 2-外汇
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//数字
//            }else{
//                $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//外汇
//            }
		$count = $mt4_trades->where($where)->count();
		$Page = new \Think\Page($count, 20);
		$Page->parameter['fo'] = $fo;
		$Page->parameter['to'] = $to;
		$show = $Page->show();// 分页显示输出
		$data = $mt4_trades->where($where)->order('TICKET desc')
			->field('TICKET,LOGIN,CMD,SL,TP,CLOSE_TIME,COMMISSION,PROFIT,COMMENT')
			->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$this->assign('data', $data);
		
		$this->assign('user', $user);
		$this->assign('page', $show);
		$this->display();
    }

}